package com.cms.recursoshumanos.activity.acceso;

import com.cms.recursoshumanos.activity.main.MainRecursosHumanosActivity;
import com.cms.recursoshumanos.constant.Constant;
import com.cms.recursoshumanos.handler.RecuperacionHandler;
import com.cms.recursoshumanos.utilities.Tool;
import com.example.frontendrecursoshumanos.R;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.ProgressDialog;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class LostUsernameActivity extends MainRecursosHumanosActivity 
{
	private static EditText emailField;
	protected static LostUsernameAsync mLostUsernameAsyncTask;
	

	private ProgressDialog dialog;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_lost_username);
		setComponents();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) 
	{
		// Inflate the menu; this adds items to the action bar if it is present.
		//getMenuInflater().inflate(R.menu.lost_username, menu);
		return true;
	}

	public void setComponents()
	{
		emailField = (EditText) findViewById(R.id.etEmail);
		
	}
	public boolean verificar_campos()
    {
    	
		if (emailField.getText().toString().equals(""))
		{

			Toast.makeText(getAppContext(),"Introduzca el Email"
					, Toast.LENGTH_LONG).show();
			return false;
		}
		else
		{
			return true;
		}
    }
	public void onResetearClick(View view)
    {
		if (verificar_campos())
		{
			String email = emailField.getText().toString();
			
			mLostUsernameAsyncTask = new LostUsernameAsync(email);
			mLostUsernameAsyncTask.execute();
		}
	}
	
	private class LostUsernameAsync extends AsyncTask<String, Void, String>
	{

		private String email;
		
    	
		public LostUsernameAsync(String email)
		{
			this.email = email;
		}
		
		@SuppressWarnings("deprecation")
		@Override
		protected void onPreExecute()
		{
			showDialog(0);
			
			dialog = ProgressDialog.show(LostUsernameActivity.this, "Espere","Cargando",true );
		}

		@Override
		protected String doInBackground(String... arg0) 
		{

			RecuperacionHandler handler = new RecuperacionHandler();
			String result = handler.getEmailLostUsername(email);
			return result;
		}
		
		@Override
		public void onPostExecute(String  result)
		{
			
			String str_result = result;
			
			if (str_result.equals(Constant.CODE_OK))
			{ 
				Toast.makeText(getAppContext(),"Se envio la solicitud satisfactoriamente"
						, Toast.LENGTH_LONG).show();
				goLogin();
			}
			else 
			{
				Tool.getInstance().showMessage(str_result);
			}
			dialog.cancel();
			this.cancel(true);
			
		}
		
	}
	
}
